<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('rooms', function (Blueprint $table) {
            $table->id();
            $table->string('room_id', 50)->unique()->comment('房间唯一标识');
            $table->string('title', 100)->comment('房间标题');
            $table->text('description')->nullable()->comment('房间描述');
            $table->enum('type', ['temporary', 'permanent'])->comment('房间类型');
            $table->unsignedBigInteger('creator_id')->comment('创建者ID');
            $table->enum('menu_source', ['default', 'custom'])->default('default')->comment('菜单来源');
            $table->unsignedBigInteger('menu_id')->nullable()->comment('关联菜单ID');
            $table->json('settings')->nullable()->comment('房间设置');
            $table->timestamp('expires_at')->nullable()->comment('过期时间');
            $table->enum('status', ['active', 'closed', 'expired'])->default('active')->comment('房间状态');
            $table->integer('max_participants')->default(50)->comment('最大参与人数');
            $table->integer('current_round')->default(1)->comment('当前轮次');
            $table->integer('total_orders')->default(0)->comment('总订单数');
            $table->timestamp('last_activity_at')->nullable()->comment('最后活动时间');
            $table->timestamps();
            
            $table->foreign('creator_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('menu_id')->references('id')->on('menus')->onDelete('set null');
            
            $table->index('room_id');
            $table->index('creator_id');
            $table->index('menu_id');
            $table->index('status');
            $table->index('type');
            $table->index('expires_at');
            $table->index('last_activity_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('rooms');
    }
};